﻿using Stylet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnWPF.Pages
{
    public class PageDataGridViewModel : Screen
    {
        protected override void OnInitialActivate()
        {
            base.OnInitialActivate();
            InitList();
        }

        public BindableCollection<DataRecord> DataRecordList { get; set; }
        public DataRecord SelectedDataRecord { get; set; }

        private void InitList()
        {
            DataRecordList = new BindableCollection<DataRecord>()
            {
                new DataRecord()
                {
                    Title="Hello",
                    Size=10,
                    RecordType= RecordType.Folder
                },
                new DataRecord()
                {
                    Title="World",
                    Size=100.122323232,
                    RecordType= RecordType.Folder
                },
                new DataRecord()
                {
                    Title="Hehe",
                    Size=200,
                    RecordType= RecordType.File,
                    IsChecked=true
                }
            };
        }


        public void AddNew()
        {
            DataRecordList.Add(new DataRecord()
            {
                Time = DateTime.Now,
                Title="New Item",
            });
        }

        public void Remove()
        {
            DataRecordList.Remove(SelectedDataRecord);
        }       

        public void Update()
        {
            SelectedDataRecord.Title = "Updated";
            SelectedDataRecord.Time = DateTime.Now;
            DataRecordList.Refresh();           
        }

        public void ShowChecked()
        {
            foreach(var item in DataRecordList)
            {
                if(item.IsChecked)
                {
                    Debug.WriteLine($"{item.Title} is Checked! Type is {item.RecordType}");
                }
            }
        }

        public bool CanRemove => SelectedDataRecord != null;
        public bool CanMoveUp => SelectedDataRecord != null;
        public bool CanMoveDown => SelectedDataRecord != null;
        public bool CanUpdate => SelectedDataRecord != null;
    }

    public class DataRecord
    {
        public bool IsChecked { get; set; }
        public DateTime Time { get; set; }
        public string Title { get; set; }
        public RecordType RecordType { get; set; }
        public string ImagePath => RecordType == RecordType.File ? "/Images/File.png" : "/Images/Folder.png";
        public double Size { get; set; }
        public bool IsTooLarger => Size > 100;
        public object Tag { get; set; }

        public override string ToString()
        {
            return Title;
        }
    }

    public enum RecordType
    {
        Folder,
        File
    }
}